package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/2/11  Time:上午 10:30.
 * description: ** .
 */
public class QJCheckBox extends androidx.appcompat.widget.AppCompatImageView {
    private boolean checked;
    private Style style;
    private OnCheckedChangeListener onCheckedChangeListener;
    private  boolean qjcb_enabled;
    public QJCheckBox(Context context) {
        this(context,null);
    }

    public QJCheckBox(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public QJCheckBox(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QJCheckBox, defStyleAttr, 0);
        checked = a.getBoolean(R.styleable.QJCheckBox_qjcb_checked, false);
        qjcb_enabled=a.getBoolean(R.styleable.QJCheckBox_qjcb_enabled,true);
        int s=a.getInt(R.styleable.QJCheckBox_qjcb_style,0);
        style=s==0?Style.arc:Style.circle;
        a.recycle();
        if(qjcb_enabled){
            setOnClickListener(clickListener);
        }

        setImage();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.qjcb_enabled=enabled;
        setImage();
        setOnClickListener(enabled?clickListener:null);
    }

    private View.OnClickListener clickListener=new OnClickListener() {
        @Override
        public void onClick(View view) {
            checked=!checked;
            setImage();
            if(onCheckedChangeListener!=null){
                onCheckedChangeListener.onCheckedChanged(QJCheckBox.this,checked);
            }
        }
    };
    private void setImage(){
        if(style==Style.arc){
            if(!checked){
                setImageResource(R.drawable.ic_cb_nor);
            }else
            setImageResource(qjcb_enabled?R.drawable.ic_cb_sel:R.drawable.ic_cb_nor_unuse);
        }else {
            if(checked){
                setImageResource(R.mipmap.icon_unselect);
            }else {
                setImageResource(qjcb_enabled?R.mipmap.icon_selected:R.drawable.icon_unselect_unuse);
            }
        }
    }
    public boolean isChecked() {
        return checked;
    }

    public void setStyle(Style style) {
        this.style = style;
        setImage();
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        setImage();
        if(onCheckedChangeListener!=null){
            onCheckedChangeListener.onCheckedChanged(QJCheckBox.this,checked);
        }
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(QJCheckBox var1, boolean var2);
    }
    public enum Style{
        circle(1),arc(0);
        public  int type;
        Style(int t) {
            this.type=t;
        }
    }
}